/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.ncn.datatypes;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Simple storage class. Holds information regarding a particular time and the wifi / cell
 * signal strengths found at the time provided.
 * @author pfritzsche
 */
public class LocationData {
    public static final String
        LOCAL = "local",
        REMOTE = "remote";
    
	private final double lat;
	private final double lng;
	private final int signal;
	private final int wifi;
	private final long timestamp;
	private final String source;
	
	/**
	 * Public constructor
	 * @param timestamp When the information was recorded, in millis since epoch
	 * @param lat latitude this data was recorded at
	 * @param lng longitude this data was recorded at
	 * @param signal Signal strength of the cell phone signal while at the given location
	 * @param wifi Wifi signal strength while at the given location
	 */
	public LocationData(long timestamp, double lat, double lng, int signal,
	        int wifi, String source) {
		this.lat = lat;
		this.lng = lng;
		this.signal = signal;
		this.wifi = wifi;
		this.timestamp = timestamp;
		this.source = source;
	}
	
	public LocationData(long timestamp, double lat, double lng, int signal, int wifi) {
        this(timestamp, lat, lng, signal, wifi, LOCAL);
    }
	
	/**
	 * Public constructor. Timestamp for data is automatically populated with current time,
	 * recorded in millis since epoch
	 * @param lat latitude this data was recorded at
	 * @param lng longitude this data was recorded at
	 * @param signal Signal strength of the cell phone signal while at the given location
	 * @param wifi Wifi signal strength while at the given location
	 */
	public LocationData(double lat, double lng, int signal, int wifi) {
		this(System.currentTimeMillis(), lat, lng, signal, wifi);
	}
	
	/**
	 * Returns a JSON representation of the current object in the form:
	 * {"location": {"lat": lat, "lng": lng}, "signal": signal, "wifi": wifi, "timestamp": timestamp}
	 * @return the JSON object
	 * @throws JSONException for malformed data
	 */
	public JSONObject toJSON() throws JSONException {
		JSONObject json = new JSONObject();
		json.put("lat", lat);
		json.put("lng", lng);
		json.put("signal", signal);
		json.put("wifi", wifi);
		json.put("timestamp", timestamp);
		return json;
	}
	
	/**
	 * Latitude getter
	 * @return the latitude at the time the location was gathered
	 */
	public double lat() { return lat; }
	
	/**
	 * Longitude getter
	 * @return the longitude at the time the location was gathered
	 */
	public double lng() { return lng; }
	
	/**
	 * Signal strength getter.
	 * @return the signal strength at the given location
	 */
	public int signal() { return signal; }
	
	/**
	 * Wifi signal strength getter.
	 * @return the signal strength at the given location
	 */
	public int wifi() { return wifi; }
	
	/**
	 * Time getter
	 * @return the time [in millis since epoch] the data was collected at
	 */
	public long timestamp() { return timestamp; }
	
	/**
     * Data source getter
     * @return the source
     */
    public String source() { return source; }
	
	/**
	 * Returns a string representation for debugging purposes.
	 */
	public String toString() {
		return timestamp + ": {" + lat + ", " + lng + "} signal=" + signal + " wifi=" + wifi + 
		        " source=" + source; 
	}
}
